package org.dromara.run.controller;

import java.text.ParseException;
import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.run.domain.bo.ActivityFormBo;
import org.dromara.run.domain.vo.ActivityFormVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.run.domain.vo.RuleVo;
import org.dromara.run.domain.bo.RuleBo;
import org.dromara.run.service.IRuleService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 跑步规则表
 *
 * @author chl
 * @date 2024-09-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/run/rule")
public class RuleController extends BaseController {

    private final IRuleService ruleService;

    /**
     * 查询跑步规则表列表
     */
    @SaCheckPermission("run:rule:list")
    @GetMapping("/list")
    public TableDataInfo<RuleVo> list(RuleBo bo, PageQuery pageQuery) {
        return ruleService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询跑步规则表列表
     *
     * @param bo 查询主体
     */
    @SaCheckPermission("run:rule:query")
    @GetMapping("/infoList")
    public R<List<RuleVo>> getInfoList(RuleBo bo) {
        return R.ok(ruleService.queryList(bo));
    }

    /**
     * 导出跑步规则表列表
     */
    @SaCheckPermission("run:rule:export")
    @Log(title = "跑步规则表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(RuleBo bo, HttpServletResponse response) {
        List<RuleVo> list = ruleService.queryList(bo);
        ExcelUtil.exportExcel(list, "跑步规则表", RuleVo.class, response);
    }

    /**
     * 获取跑步规则表详细信息
     *
     * @param ruleId 主键
     */
    @SaCheckPermission("run:rule:query")
    @GetMapping("/{ruleId}")
    public R<RuleVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long ruleId) {
        return R.ok(ruleService.queryById(ruleId));
    }

    /**
     * 新增跑步规则表
     */
    @SaCheckPermission("run:rule:add")
    @Log(title = "跑步规则表", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody RuleBo bo) throws ParseException {
        return toAjax(ruleService.insertByBo(bo));
    }

    /**
     * 修改跑步规则表
     */
    @SaCheckPermission("run:rule:edit")
    @Log(title = "跑步规则表", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody RuleBo bo) throws ParseException {
        return toAjax(ruleService.updateByBo(bo));
    }

    /**
     * 删除跑步规则表
     *
     * @param ruleIds 主键串
     */
    @SaCheckPermission("run:rule:remove")
    @Log(title = "跑步规则表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ruleIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ruleIds) {
        return toAjax(ruleService.deleteWithValidByIds(List.of(ruleIds), true));
    }
}
